package fy.uap.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import com.seeyon.ctp.util.UUIDLong;

import fy.config.FyConfig;
import fy.uap.bean.ApptitudeBean;
import fy.uap.bean.ProviderImportFormBean;
import fy.util.DBUtil;
import fy.util.MyUtil;


public class MergerProviderInfoFormDao {
	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(MergerProviderInfoFormDao.class);

	private final static String uapUserID = "0001AA10000000009KX7";

	private ProviderImportFormBean mergerProviderFormBean;

	private boolean writeAptitudeSuccess = true;

	private List<String> companyList;

	private final static String update_fy_supplier_vendor = "update fy_supplier_vendor set ctrade=?,cvenregcode=?,dorf=?,cqualitycond=?,cvenbank=?,cvenaccount=?,cmoneytype=?,cpurbillcode=?,couttype=?,cvenaddress=?,cvenpostcode=?, cvenperson=?, cvenphone=?,cvenfax=?,cvenemail=?,cveniaddress=?,ccuspaycond=?,ccontent=?,cycle=?,ccreateperson=?, ts=?, cVenBankMemo=?,enterprise_type=?,registered_capital=?,establish_time=?,annual_sales=?,worker_number=?,premises=?,cvenlperson=?,legal_person_id_card=? where cvencode=? and (dr='0' or dr is null) ";
	private final static String update_fy_supplier_vendor_invclass = "update fy_supplier_vendor_invclass i set i.bankaccountname=?,i.bankcode = ?, i.bank = ?,i.bankaccount = ?,i.currency = ? where i.pk_vendor = ( select v.pk_vendor from fy_supplier_vendor v where v.cvencode = ? and nvl(v.dr,0) = 0) and i.pk_corp=?";
	private final static String insert_log = "insert into fy_supplier_vendor_history(pk_venhis, pk_vendor, pk_corp, pk_invclass, changetype, changememo, changevouch, changecode, changer, ts, changedate, creater) values (?, (select v.pk_vendor from fy_supplier_vendor v where v.cvencode = ? and nvl(v.dr,0) = 0), ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";

	private List<ApptitudeBean> apptitudeBeans;

	public void setApptitudeBeans(List<ApptitudeBean> apptitudeBeans) {
		this.apptitudeBeans = apptitudeBeans;
	}

	public void setCompanyList(List<String> companyList) {
		this.companyList = companyList;
	}

	public void setMergerProviderFormBean(
			ProviderImportFormBean mergerProviderFormBean) {
		this.mergerProviderFormBean = mergerProviderFormBean;
	}

	public boolean save() {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		PreparedStatement pst1 = null;
		PreparedStatement pst2 = null;
		try {
			conn = DBUtil.connect(FyConfig.getUAP_DB_CONFIG());

			DBUtil.setAutoCommit(conn, false);

			pst = conn.prepareStatement(update_fy_supplier_vendor);
			pst1 = conn.prepareStatement(update_fy_supplier_vendor_invclass);
			pst2 = conn.prepareStatement(insert_log);

			/**
			 * 供应商信息变更
			 */
			pst.setString(1, mergerProviderFormBean.getCompanyType());
			pst.setString(2, mergerProviderFormBean.getTaxRegister());
			pst.setString(3, mergerProviderFormBean.getProperty());
			pst.setString(4, mergerProviderFormBean.getQualitySystem());
			pst.setString(5, mergerProviderFormBean.getCvenbank());
			pst.setString(6, mergerProviderFormBean.getBankAccount());
			pst.setString(7, mergerProviderFormBean.getCmoneytype());
			pst.setString(8, mergerProviderFormBean.getCpurbillcode());
			pst.setString(9, mergerProviderFormBean.getCouttype());
			pst.setString(10, mergerProviderFormBean.getSupplyAddress());
			pst.setString(11, mergerProviderFormBean.getPostCode());
			pst.setString(12, mergerProviderFormBean.getLinkman());
			pst.setString(13, mergerProviderFormBean.getTel());
			pst.setString(14, mergerProviderFormBean.getFax());
			pst.setString(15, mergerProviderFormBean.getEmail());
			pst.setString(16, mergerProviderFormBean.getAddress());
			pst.setString(17, mergerProviderFormBean.getCcuspaycond());
			pst.setString(18, mergerProviderFormBean.getCcontent());
			pst.setString(
					19,
					MyUtil.getLimitLengthString(
							mergerProviderFormBean.getSupplyCycle(), 20, "…"));
			pst.setString(20, uapUserID);
			pst.setString(21, mergerProviderFormBean.getApplicationDate());
			pst.setString(22, mergerProviderFormBean.getcVenBankMemo());
			
			pst.setString(23, mergerProviderFormBean.getQylb());
			pst.setString(24, mergerProviderFormBean.getZczj());
			pst.setString(25, mergerProviderFormBean.getClsj());
			pst.setString(26, mergerProviderFormBean.getNxse());
			pst.setString(27, mergerProviderFormBean.getZgrs());
			pst.setString(28, mergerProviderFormBean.getJycs());
			pst.setString(29, mergerProviderFormBean.getFrdb());
			pst.setString(30, mergerProviderFormBean.getFrdbsfz());
			
			pst.setString(31, mergerProviderFormBean.getProviderCode());
			pst.executeUpdate();

			for (int i = 0, length = companyList.size(); i < length; i++) {
				/**
				 * 银行信息变更
				 */
				pst1.setString(1, mergerProviderFormBean.getBankAccountName());
				pst1.setString(2, mergerProviderFormBean.getBankCode());
				pst1.setString(3, mergerProviderFormBean.getcVenBankMemo());
				pst1.setString(4, mergerProviderFormBean.getBankAccount());
				pst1.setString(5, mergerProviderFormBean.getCmoneytype());
				pst1.setString(6, mergerProviderFormBean.getProviderCode());
				pst1.setString(7, companyList.get(i));
				pst1.addBatch();

				/**
				 * 日志
				 */
				pst2.setString(1, String
						.valueOf(UUIDLong
								.longUUID()));
				pst2.setString(2, mergerProviderFormBean.getProviderCode());
				pst2.setString(3, companyList.get(i));
				pst2.setString(4, "");
				pst2.setString(5, "变更");
				pst2.setString(
						6,
						new StringBuilder()
								.append("供应商【")
								.append(mergerProviderFormBean
										.getProviderName()).append("】信息发生变更")
								.toString());
				pst2.setString(7, "OA供应商信息变更申请表");
				pst2.setString(8, this.mergerProviderFormBean.getNumber());
				pst2.setString(9, uapUserID);
				pst2.setString(10, MyUtil.getNowStr());
				pst2.setString(11, MyUtil.getTodayDateStr());
				pst2.setString(12, mergerProviderFormBean.getMemberCode());
				pst2.addBatch();
			}
			pst1.executeBatch();
			pst2.executeBatch();

			conn.commit();

			writeAptitudeSuccess = true;
//					AptitudeDao.writeAptitude(conn,
//					apptitudeBeans, mergerProviderFormBean.getProviderCode(),
//					"信息变更" + mergerProviderFormBean.getNumber(),
//					mergerProviderFormBean.getMemberCode());

			success = true;

		} catch (Exception e) {
			success = false;
			DBUtil.rollback(conn);
			e.printStackTrace();
			log.error("供应商信息变更表数据导入UPA异常", e);
		} finally {
			DBUtil.setAutoCommit(conn, true);
			DBUtil.close(pst1);
			DBUtil.close(pst2);
			DBUtil.close(pst, conn);
		}
		return success;
	}

	public boolean isWriteAptitudeSuccess() {
		return writeAptitudeSuccess;
	}
}
